package com.xayoudao.uneng.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xayoudao.uneng.R;

/**
 * 设备唯一id 和md5算法 
 * 
 */
public class DeviceUtils {
	private static final String TAG = "DeviceUtils";

	public static String deviceUUID(Context context) {
		final TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		final String tmDevice, tmSerial, androidId;
		tmDevice = "" + tm.getDeviceId();
		tmSerial = "" + tm.getSimSerialNumber();
		androidId = ""
				+ android.provider.Settings.Secure.getString(
						context.getContentResolver(),
						android.provider.Settings.Secure.ANDROID_ID);
		UUID deviceUUID = new UUID(androidId.hashCode(),
				((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());

		return deviceUUID.toString();
	}
	/**
	 * 取得设备的UUID 去掉-连接符
	 * @param context
	 * @return   设备的UUID
	 */
	public static String getUUID(Context context) {
		
		StringBuilder builder = new StringBuilder() ;
		String uuids[] = deviceUUID(context).split("-") ;
		for(int i=0;i<uuids.length;i++){
			builder.append(uuids[i]) ;
		}
		return builder.toString();
	}
	
	/**
	 * MD5加密算法
	 * 
	 * @param str
	 * @return
	 */
	public static String toMD5(String str) {
		try {
			MessageDigest algorithm = MessageDigest.getInstance("MD5");
			algorithm.reset();
			algorithm.update(str.getBytes("UTF-8"));
			return toHexString(algorithm.digest());
		} catch (NoSuchAlgorithmException e) {
			Log.v(TAG, "toMd5(): " + e);
			throw new RuntimeException(e);
		} catch (UnsupportedEncodingException e) {
			Log.v(TAG, "toMd5(): " + e);
		}
		return "";
	}

	private static String toHexString(byte[] bytes) {
		StringBuilder hexString = new StringBuilder();
		
		for (int j = 0; j < bytes.length; j++) {
			if ((bytes[j] & 0xff) < 0x10) {
				hexString.append("0") ;
			}
			hexString.append(Long.toString(bytes[j]& 0xff,16));
		}
//	
//		for (byte b : bytes) {
//			hexString.append(Integer.toHexString(0xFF & b));
//		}
		return hexString.toString();
	}
	
	// 获取百度推送的AppKey
    public static String getMetaValue(Context context, String metaKey) {
        Bundle metaData = null;
        String apiKey = null;
        if (context == null || metaKey == null) {
        	return null;
        }
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
                    context.getPackageName(), PackageManager.GET_META_DATA);
            if (null != ai) {
                metaData = ai.metaData;
            }
            if (null != metaData) {
            	apiKey = metaData.getString(metaKey);
            }
        } catch (NameNotFoundException e) {

        }
        return apiKey;
    }

    private static DefineCustomProgressDialog myprogressDialog = null;
	
	/**
	 * 显示loading
	 */
	public static void startProgressDialog(Activity context ,String message){
		
			myprogressDialog = DefineCustomProgressDialog.createDialog(context);
			myprogressDialog.setMessage(message);
		
		
		myprogressDialog.show();
	}
	/**
	 * 关闭loading
	 */
	public static void stopProgressDialog(){
		if (myprogressDialog != null&&myprogressDialog.isShowing()){
			myprogressDialog.dismiss();
			myprogressDialog = null;
		}
	}
	
	
	/**
	 * 显示消息
	 * @param toastContent
	 * @param context
	 */
	public static void showMessage(String toastContent,Context context){
		LayoutInflater inflater =  LayoutInflater.from(context) ;
		// 根据指定的布局文件创建一个具有层级关系的View对象
		View layout = inflater.inflate(R.layout.toast, null);
		LinearLayout root = (LinearLayout) layout
				.findViewById(R.id.toast_layout_root);
		root.getBackground().setAlpha(150);// 0~255透明度值
		
		TextView text = (TextView) layout.findViewById(R.id.text);
		text.setText(toastContent);
	
		Toast toast = new Toast(context);
		// 设置Toast的位置
		// toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		// toast.setGravity(Gravity.BOTTOM, 0, 0);
		toast.setDuration(Toast.LENGTH_SHORT);
		// 让Toast显示为我们自定义的样子
		toast.setView(layout);
		toast.show();
	}
	
	/**
	 * 没有到摇奖时间就显示提示框
	 * @param context
	 */
	public static void AlertDialogInfo(Context context) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context) ;
		builder.setIcon(R.drawable.icon) ;
		builder.setTitle("提醒!") ;
		builder.setMessage("请在摇奖时间内摇奖，详情请见活动说明") ;
		builder.setNegativeButton("取消", null) ;
		builder.create().show() ;
	}
}
